package com.zhoug.widget.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.zhoug.widget.R;

/**
 * 自定义圆形/圆角图片 默认圆形
 */
public class RoundImageView extends android.support.v7.widget.AppCompatImageView {
    private static final String TAG = "RoundImageView";

    /**
     * 是否为圆形 为true时corners属性无效*
     */
    private boolean isRound = true;
    /**
     * 四周角度 isRound==false时生效
     */
    private int corners = 0;

    /**
     * 半径
     */
    private int mRadius;

    private int width;
    private int height;


    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        corners = typedArray.getDimensionPixelSize(R.styleable.RoundImageView_corners, 0);
        isRound = typedArray.getBoolean(R.styleable.RoundImageView_isRound, true);

        typedArray.recycle();

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        width = getWidth();
        height = getHeight();

        //计算半径
        int width = getWidth();
        int height = getHeight();
        mRadius = (width > height ? height : width) / 2;
    }

    private Path getPath() {
        Path path = new Path();
        if (isRound) {
            path.addCircle(getWidth() / 2, getHeight() / 2, mRadius, Path.Direction.CCW);
        } else if (corners > 0) {
            if (corners >= mRadius) {
                path.addCircle(getWidth() / 2, getHeight() / 2, mRadius, Path.Direction.CCW);
            } else {
                //裁剪4角
                path.moveTo(corners, 0);
                //线
                path.lineTo(width - corners, 0);
                //贝塞尔曲线分别以4个顶点作为控制点
                path.quadTo(width, 0, width, corners);

                path.lineTo(width, height - corners);
                path.quadTo(width, height, width - corners, height);

                path.lineTo(corners, height);
                path.quadTo(0, height, 0, height - corners);

                path.lineTo(0, corners);
                path.quadTo(0, 0, corners, 0);


            }

        }

        return path;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //设置画圆或者画圆角图片时才裁剪画布
        if (isRound || corners > 0) {
            canvas.clipPath(getPath());
//            Log.d(TAG, "剪切");
        }


        super.onDraw(canvas);
    }


    public void setRound(boolean round) {
        isRound = round;
    }

    public void setCorners(int corners) {
        this.corners = corners;
    }
}
